package com.example.farmeri.ui.farm_data;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.farmeri.R;
import com.example.farmeri.adapter.FarmDataAdapter;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.tools.IdentUtils;
import com.example.farmeri.ui.base.BaseFragment;
import com.example.farmeri.adapter.MyItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FarmDataFragment extends BaseFragment {

    @BindView(R.id.farm_data_recycle_view)
    RecyclerView recyclerView;

    @BindView(R.id.farm_data_srl)
    SwipeRefreshLayout srl;

    @BindView(R.id.hint)
    TextView hint;

    private Unbinder unbinder;

    static FarmDataFragment fragment;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_farm_data, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (IdentUtils.hasFarmDataPower()) {
            hint.setVisibility(View.INVISIBLE);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerViewAddData();
        }else {
            hint.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.INVISIBLE);
        }
        initToolBar(R.id.fragment_farm_data_toolbar,"农田数据",false,false);
        srl.setEnabled(false);
        //刷新数据
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                srl.setRefreshing(false);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        if (IdentUtils.hasFarmDataPower()) {
//            adapter.cleanData();
//            adapter.fetchData(1,999);
        }
    }
    FarmDataAdapter adapter;
    private void recyclerViewAddData() {
        adapter = new FarmDataAdapter(getContext(),R.layout.item_farm_data);
        MyItemDecoration decoration = new MyItemDecoration(MyItemDecoration.SHORT_LINE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(decoration);
    }

    public static FarmDataFragment newInstance() {
        if (fragment == null){
            return new FarmDataFragment();
        }
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        recyclerView = null;
        fragment = null;
    }
}
